
let config = require('./config')

let _requestFunc = function(options, token, successCallback, errorCallback) {
  let hostName = options.hostName || 'schoolTransport';
    if (!(options instanceof Object)) {
      return reject(new Error('请求参数错误'))
    }
    let headers = Object.assign(options.headers || {}, {
      // 公共header
    })
    let data = options.data || {}
    data.token = token || '';
    let requestParams = Object.assign(options, {
      header: headers,
      timeout: 30000,
      dataType: 'json',
      url: getUrl(options),
      data,
      success: (response) => {
        if (!response.data) {
          errorToast('请求错误')
          errorCallback(new Error('请求错误'))
          return;
        }
        if(hostName === 'schoolTransport' && response.data.code == 1000) {
            wx.reLaunch({url: '/pages/login/login'})
            errorCallback(new Error('授权失败'))
            return;
        }

        if(hostName === 'schoolTransport' && response.data.code == 1003) {
          wx.navigateTo({
            url: 'pages/userInfo/userInfo',
          })
          errorCallback(new Error('请选择学校'));
          return;
      }
        if (
          (hostName === 'schoolTransport' && response.data.code != 0) || 
          (hostName === 'xyt' && response.data.code != 1)
        ) {
          errorToast(response.data.msg || '请求错误')
          errorCallback(new Error(response.data.msg || '请求错误'))
        }
        successCallback(response.data.data);
      },
      error: (error) => {
        errorToast(error.message || '请求错误')
        reject(error)
      },
      complete: () => {

      }

    })
    wx.request(requestParams)
}

let requestHandle = (options, success, error) => {
  if(options.auth) {
    getApp().getApiToken((token) => {
      _requestFunc(options, token, (data) => {
        success(data)
      }, (e) => {
        error(e)
      })
    })

  } else {
    try {
      var token = wx.getStorageSync('apiToken')
    } catch (e) {
      var token  = null;
    }
    console.log(token);
    _requestFunc(options, token, (data) => {
      success(data)
    }, (e) => {
      error(e)
    })
  }
}

let request = function (options) {
  return new Promise((resolve, reject) => {
    requestHandle(options,(data) => {
      resolve(data)
    }, (e) => {
      reject(e)
    })
  })
}
let getUrl = (options) => {
  let host = config[options.hostName || 'schoolTransport']
  let url = String(options.url)
  if (!url.startsWith('/')) {
    url = '/' + url;
  }
  return host + url
}

let errorToast = (message) => {
  wx.hideLoading()
  wx.showToast({
    title: message,
    icon: 'error',
    duration: 2000
  })
}
module.exports = {
  homeMenuApi() {

    return request({
      url: '/home/bannerMenu',
      method: 'post',
    })
  },
  shopList(query) {
    return request({
      url: '/home/shopList',
      data: query,
      method: 'post',
      auth: true,
    })
  },
  addressEdit(query) {
    return request({
      url: '/user/addressEdit',
      data: query,
      method: 'post',
      auth: true,
    })
  },
  addressDetail(query) {
    return request({
      url: '/user/addressDetail',
      data: query,
      method: 'post',
      auth: true,
    })
  },
  addressList(query) {
    return request({
      url: '/user/addressList',
      data: query,
      method: 'post',
      auth: true,
    })
  },
  addressSet(query) {
    return request({
      url: '/user/addressSet',
      data: query,
      method: 'post',
      auth: true,
    })
  },
  shop(data) {
    return request({
      url: 'home/shop',
      method: 'post',
      auth: true,
      data
    })
  },
  cartSet(data) {
    return request({
      url: '/user/cartSet',
      method: 'POST',
      auth: true,
      data
    })
  },
  getApiToken(token) {
    return request({
      url: 'getMobileToken/' + token,
      hostName: 'xyt',
      method: 'get'
    }) 
  },
  specification(data) {
    return request({
      url: '/home/specification',
      data: data,
      method: 'post'
    })
  },
  cartDel(data) {
    return request({
      url: '/user/cartDel',
      method: 'POST',
      auth: true,
      data
    })
  },
  confirmOrder(data) {
    return request({
      url: 'order/orderSure',
      method: 'POST',
      auth: true,
      data
    })
  },
  submitOrder(data) {
    return request({
      url: '/order/orderCreate',
      method: 'POST',
      auth: true,
      data
    })
  },
  
}